package com.cskaoyan.service.statisticalreports;


import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bean.statisticalreportsVO.AdminStatGoodsVO;
import com.cskaoyan.module.bean.statisticalreportsVO.AdminStatOrderVO;
import com.cskaoyan.module.bean.statisticalreportsVO.AdminStatUser4GetVO;
import com.cskaoyan.module.pojo.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import java.util.stream.Collectors;

@Service
public class StatisticalReportsServeImpl implements StatisticalReportsServe {
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;


    @Override
    public BaseRespVo getStatisticalReportsUser() {
        AdminStatUser4GetVO adminStatUser4GetVO = new AdminStatUser4GetVO();

        ArrayList<java.lang.String> strings = new ArrayList<>();
        strings.add("day");
        strings.add("users");
        adminStatUser4GetVO.setColumns(strings);
        ArrayList<AdminStatUser4GetVO.RowsDTO> rows = new ArrayList<>();
        List<Date> addTimeList = getAddTimeList();

        for (Date date : addTimeList) {
            MarketUserExample marketUserExample = new MarketUserExample();
            MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
            criteria.andAddTimeEqualTo(date);
            Long l = marketUserMapper.countByExample(marketUserExample);
            AdminStatUser4GetVO.RowsDTO rowsDTO = new AdminStatUser4GetVO.RowsDTO();
            rowsDTO.setUsers(l.intValue());
            rowsDTO.setDay(date.toString());
            rows.add(rowsDTO);

        }
        adminStatUser4GetVO.setRows(rows);


        return BaseRespVo.ok(adminStatUser4GetVO);
    }

    /**
     * //当天订单金额总额  求和 actual_price 条件为 add_time
     *         @JsonProperty("amount")
     *         private Integer amount;
     *
     *      //订单数量  通过order表的id条件为add_time
     *         @JsonProperty("orders")
     *         private Integer orders;
     *
     *      //当日下单用户总数  user_id 消除相同的id后统计数量
     *         @JsonProperty("customers")
     *         private Integer customers;
     *      //订单成交日 获取add_time
     *         @JsonProperty("day")
     *         private String day;
     *      //客单价=总销售额/总顾客数
     *         @JsonProperty("pcr")
     *         private Integer pcr;
     * @return
     */

    @Override
    public BaseRespVo getStatisticalReportsOrder() {
        AdminStatOrderVO adminStatOrderVO = new AdminStatOrderVO();
        ArrayList<AdminStatOrderVO.RowsDTO> rowsDTOS = new ArrayList<>();
       //"day","orders","customers","amount","pcr"
        java.lang.String[] strings = {"day", "orders", "customers", "amount", "pcr"};
        adminStatOrderVO.setColumns(strings);
        //获取一个add_Time的list
        MarketOrderExample marketOrderExample1 = new MarketOrderExample();
        marketOrderExample1.createCriteria().andPayTimeIsNotNull();
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample1);
        ArrayList<Date> dates = new ArrayList<>();
        for (MarketOrder marketOrder : marketOrders) {
            dates.add(marketOrder.getAddTime());
        }
        for (Date date1 : dates) {
            MarketOrderExample marketOrderExample2 = new MarketOrderExample();
            marketOrderExample2.createCriteria().andAddTimeEqualTo(date1);
            AdminStatOrderVO.RowsDTO rowsDTO = new AdminStatOrderVO.RowsDTO();
            rowsDTO.setDay(date1.toString());
            //订单数量  通过order表的id条件为add_time
            Long orders = marketOrderMapper.countByExample(marketOrderExample2);
            rowsDTO.setOrders(orders.intValue());
            //当天订单金额总额  求和 actual_price 条件为 add_time
            List<MarketOrder> marketOrders1 = marketOrderMapper.selectByExample(marketOrderExample2);
            ArrayList<BigDecimal> actualPrices = new ArrayList<>();
            //当日下单用户总数  user_id 消除相同的id后统计数量
            ArrayList<Integer> integers = new ArrayList<>();
            for (MarketOrder marketOrder : marketOrders1) {
                actualPrices.add(marketOrder.getActualPrice());
                integers.add(marketOrder.getUserId());
            }
            //当日下单用户总数  user_id 消除相同的id后统计数量
            Long count = integers.stream().distinct().count();
            rowsDTO.setCustomers(count.intValue());
            //当天订单金额总额


            Double sum=0.0;
            for (BigDecimal actualPrice : actualPrices) {
                String s1 = actualPrice.toString();
                double v = Double.parseDouble(s1);
                    sum+=v;
            }
            rowsDTO.setAmount(new BigDecimal(sum.toString()));
            Double pcr=sum/count.intValue();
            rowsDTO.setPcr(new BigDecimal(pcr.toString()));

            rowsDTOS.add(rowsDTO);

        }
        adminStatOrderVO.setRows(rowsDTOS);



        return BaseRespVo.ok(adminStatOrderVO);
    }

    /**
     *     @JsonProperty("amount")
     *     //下单货品总额
     *     private Integer amount;
     *    //订单量
     *     @JsonProperty("orders")
     *     private Integer orders;
     *     //下单日期
     *     @JsonProperty("day")
     *     private String day;
     *     //下单货品数量
     *     @JsonProperty("products")
     *     private Integer products;
     * @return
     */
    @Override
    public BaseRespVo getStatisticalReportsGoods() {
        AdminStatGoodsVO adminStatGoodsVO = new AdminStatGoodsVO();

        java.lang.String[] strings = {"day","orders","products","amount"};
        adminStatGoodsVO.setColumns(strings);



    //获取一个add_Time的list
        MarketOrderGoodsExample marketOrderExample = new MarketOrderGoodsExample();
        marketOrderExample.createCriteria().andAddTimeIsNotNull();
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(marketOrderExample);
        ArrayList<AdminStatGoodsVO.RowsDTO> rowsDTOS = new ArrayList<>();
        ArrayList<Date> dates = new ArrayList<>();
        for (MarketOrderGoods marketOrderGood : marketOrderGoods) {
            dates.add(marketOrderGood.getAddTime());
        }
        for (Date date : dates) {
            AdminStatGoodsVO.RowsDTO rowsDTO = new AdminStatGoodsVO.RowsDTO();
            rowsDTO.setDay(date.toString());
            MarketOrderGoodsExample marketOrderExample1 = new MarketOrderGoodsExample();
            marketOrderExample1.createCriteria().andAddTimeEqualTo(date);
            List<MarketOrderGoods> marketOrderGoods1 = marketOrderGoodsMapper.selectByExample(marketOrderExample1);
            //商品总价
            Double amount=0.0;
            ArrayList<BigDecimal> prices = new ArrayList<>();
            //订单量order_id 求count
            Integer ordersSum=null;
            ArrayList<Integer> ordersSums = new ArrayList<>();

            //下单货品数量 goods_id求count（*）
            Integer products=null;
            ArrayList<Integer> goodsSums = new ArrayList<>();
            for (MarketOrderGoods orderGoods : marketOrderGoods1) {
                prices.add(orderGoods.getPrice());
                ordersSums.add(orderGoods.getOrderId());
                goodsSums.add(orderGoods.getGoodsId());

            }
            for (BigDecimal price : prices) {
                java.lang.String s1 = price.toString();
                double v = Double.parseDouble(s1);
                amount+=v;
            }
            rowsDTO.setAmount(amount);
            Long count = ordersSums.stream().count();
            ordersSum=count.intValue();
            rowsDTO.setOrders(ordersSum);
            Long count1 = goodsSums.stream().count();
            products=count1.intValue();
            rowsDTO.setProducts(products);
            rowsDTOS.add(rowsDTO);

        }
        adminStatGoodsVO.setRows(rowsDTOS);

        return BaseRespVo.ok(adminStatGoodsVO);
    }


    private List<Date> getAddTimeList() {
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        //先获取注册时间
        criteria.andAddTimeIsNotNull();

        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        ArrayList<Date> days = new ArrayList<>();

        for (MarketUser marketUser : marketUsers) {
            days.add(marketUser.getAddTime());
        }
        List<Date> collect = days.stream().distinct().collect(Collectors.toList());

        return collect;
    }
}
